var localhost = '172.16.5.236'
var styleFunction = function (feature) {
    var id = feature.get("status");
    var style = null;
    if (id == 0) {
        style = new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: 'yellow',
                width: 5
            })
        });
    } else if (id == 2) {
        style = new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: 'red',
                width: 8
            })
        });
    } else {
        style = new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: 'blue',
                width: 2
            })
        });
    }
    return [style]
}

function tramSelect() {
    map.getOverlays().clear();
    name_tram = jQuery("#select1  option:selected").text();
    var Mystatus;
    if (name_tram == '所有等级路段') {
        vector.setSource(vectorSource);
        vector.setStyle(styleFunction);
    } else if (name_tram == '畅行无阻路段') {
        Mystatus = 0;
    } else if (name_tram == '正常行驶路段') {
        Mystatus = 1;
    } else if (name_tram == '严重拥堵路段') {
        Mystatus = 2;
    }
    $.ajax({
        url: "http://"+localhost+":8000/web/statusquery/", // 相当于form 中的 action
        type: 'POST', // 相当于form 中的 method
        data: {
            'status': Mystatus
        }, // 数据
        dataType: "json", // 注意这个定义的是返回值的类型，不是发送数据的类型，如果返回类型不是json类型，则会进入error函数
        headers: {
            "X-CSRFToken": "{{ csrf_token }}"
        }, //Django中表单提交都需要csrf验证
        success: function (data) { // 后端返回成功后的回调函数,data为后端传来的数据
            var source = new ol.source.Vector();
            for (var i = 0; i < data.features.length; i++) {
                if (data.features[i].properties.status == Mystatus) {
                    var coords = data.features[i].geometry.coordinates;
                    var geom = new ol.geom.LineString(coords);
                    geom.transform('EPSG:4326', 'EPSG:3857');
                    var feature = new ol.Feature(geom);
                    feature.setProperties({
                        "status": Mystatus
                    });
                    source.addFeature(feature);
                }
            }
            vector.setSource(source)
            vector.setStyle(styleFunction)
        },
        error: function () {
            alert('没有获取数据')
        }
    })
}


